<template>
     <div class="content">
        <el-container>
            <el-header height="40px">
          <el-page-header @back="goBack"  style="margin-top: 10px"></el-page-header>
        </el-header>
        <div class="text">请先填写您的个人信息哦🥰🥰</div>
        <div class="down">
            <el-main style="height: 800px;">
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="用户名" prop="username">
                        <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="pass">
                        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="姓名" prop="name">
                        <el-input type="text" v-model="ruleForm.name" autocomplete="off"></el-input>
                    </el-form-item> -->
                    <el-form-item label="手机号" prop="phone">
                        <el-input type="text" v-model="ruleForm.phone" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="年龄" prop="age">
                        <el-input type="number" v-model.number="ruleForm.age"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button style="background-color: #fced6e" @click="resetForm('ruleForm')">重置</el-button>
                        <el-button style="background-color: #009be2" type="info" plain @click="submitForm('ruleForm')">注册</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </div>
        </el-container>
    </div>
</template>
<script>
    export default {
        name: "RiderRegister",
        data() {
            const validateUsername = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入用户名'));
                } else {
                    callback();
                }
            };


            const validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };

            const validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };

            const validateName = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入姓名'));
                } else {
                    callback();
                }
            };

            const validatePhone = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入手机号'));
                } else {
                    setTimeout(()=> {
                        if(value === '123'){
                            callback(new Error('该手机号已注册！！'))
                        }else {
                            callback();
                        }
                    },1000)

                }
            };

            const checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 500);
            };

            return {
                ruleForm: {
                    username:'',
                    pass: '',
                    checkPass: '',
                    name:'',
                    phone:'',
                    age: ''
                },
                rules: {
                    username: [
                        { validator:validateUsername,trigger: 'blur'}
                    ],
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    name: [
                        { validator:validateName,trigger: 'blur'}
                    ],
                    phone: [
                        { validator:validatePhone,trigger: 'blur'}
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                const that = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(confirm("确认信息无误吗？")){
                            var param={
                                name:that.ruleForm.username,
                                password: that.ruleForm.pass,
                                phone: that.ruleForm.phone,
                                age: that.ruleForm.age
                            };
                            this.axios.post("http://localhost:8084/riderRegister", param)
                                .then(resp => {
                                    if (resp.data === "success") {
                                        this.$message({
                                            message: '注册成功！！！',
                                            type: 'success'
                                        });
                                        this.$router.push("/toRiderLogin")
                                    } else {
                                        this.$message({
                                            message: '注册失败！！！',
                                            type: 'error'
                                        });
                                    }
                                })
                            this.$router.push("/")
                        }else {
                            return false;
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            goBack() {
                this.$router.push("/registerType")
            }
        }
    }
</script>


<style scoped>
.content {
  /*background-color: #009be2;*/
  background: url(../../public/upload/img9.jpg) no-repeat;
  background-size: cover;
  height: 100vh;
  width: 100vw;
  color: black;
}
.text {
  position: absolute;
  top: 20px;
  right: 35%;
  font-size: 36px;
  color: #000000;
  /*color: black;*/
  font-weight: 600;

}
.register {
  text-align: center;
}
    .el-form{
      /*background-color: rgba(0,0,0,0.3);*/
        width: 500px;
        height: 200px;
        margin: 20px auto;
    }

    /*.el-container{*/
    /*    height: 680px;*/
    /*}*/
    .el-main {
        /*background-color: #009be2;*/
        color: #333;
        text-align: center;
        line-height: 160px;

    }

    .el-header {
        /*background-color: #009be2;*/
        color: #333;
        text-align: center;
    }

    .el-footer{
        background-color: #B3C0D1;
        color: #333;
    }
    .el-main::-webkit-scrollbar {
      display: none;
    }
   /deep/ .el-form-item__label {
      color: black;
    }
</style>
